<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11
 * Time: 22:26
 */

namespace App\Api;

use App\Domain\Examples\SpotView as DomainCURD;

use PhalApi\Api;

/**
 *☛☛景点管理√☚☚
 */
class SpotView extends Api
{
    public function getRules()
    {
        return array(
            'insert' => array(
                'title' => array('name' => 'title', 'require' => true, 'desc' => '标题'),
                'small_img' => array('name' => 'small_img', 'require' => true, 'desc' => '预览图图片地址'),
                'sort' => array('name' => 'sort', 'require' => true, 'desc' => '排序越大越靠前'),
                'score' => array('name' => 'score', 'require' => true, 'desc' => '评分'),
                'tag' => array('name' => 'tag', 'desc' => '标签多个用,分隔'),
                'price_start' => array('name' => 'price_start', 'require' => true, 'desc' => '起始价格如果0免费'),
                'spot_type' => array('name' => 'spot_type', 'require' => true, 'desc' => '景点对应的分类id'),
                'brief_desc' => array('name' => 'brief_desc', 'require' => true, 'desc' => '简介'),
                'customtel' => array('name' => 'customtel', 'require' => true, 'desc' => '客服电话'),
                'details' => array('name' => 'details', 'require' => true, 'desc' => '详情'),
                'address' => array('name' => 'address', 'desc' => '地址'),
                'images' => array('name' => 'images', 'desc' => '多张图用;隔开'),
                'opentime' => array('name' => 'opentime', 'require' => true, 'desc' => '开店时间'),
            ),
            'update' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => 'ID'),
                'title' => array('name' => 'title', 'desc' => '标题'),
                'small_img' => array('name' => 'small_img', 'desc' => '预览图图片地址'),
                'sort' => array('name' => 'sort', 'desc' => '排序越大越靠前'),
                'score' => array('name' => 'score', 'desc' => '评分'),
                'tag' => array('name' => 'tag', 'desc' => '标签多个用,分隔'),
                'price_start' => array('name' => 'price_start', 'desc' => '起始价格如果0免费'),
                'spot_type' => array('name' => 'spot_type', 'desc' => '景点对应的分类id'),
                'brief_desc' => array('name' => 'brief_desc', 'desc' => '简介'),
                'customtel' => array('name' => 'customtel', 'desc' => '客服电话'),
                'details' => array('name' => 'details', 'desc' => '详情'),
                'address' => array('name' => 'address', 'desc' => '地址'),
                'images' => array('name' => 'images', 'desc' => '多张图用;隔开'),
                'opentime' => array('name' => 'opentime', 'desc' => '开店时间'),
            ),
            'get' => array(
                'id' => array('name' => 'id', 'require' => true, 'min' => 1, 'desc' => 'ID'),
            ),
            'delete' => array(
                'id' => array('name' => 'id', 'require' => true, 'min' => 1, 'desc' => 'ID'),
            ),
            'getList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'min' => 1, 'default' => 1, 'desc' => '第几页'),
                'perpage' => array('name' => 'perpage', 'type' => 'int', 'min' => 1, 'max' => 20, 'default' => 10, 'desc' => '分页数量'),
                'cityid' => array('name' => 'cityid', 'desc' => '当前城市的id'),
                'word' => array('name' => 'word', 'desc' => '搜索关键词景点名称'),
            )
        );
    }

    /**
     * 插入数据
     * @desc 向数据库插入一条纪录数据
     * @return int id 新增的ID
     * @return boolean result 添加结果
     * @return string msg 结果描述
     */
    public function insert()
    {
        $rs = array();

        $newData = array(
            'title' => $this->title,
            'small_img' => $this->small_img,
            'sort' => $this->sort,
            'score' => $this->score,
            'tag' => $this->tag,
            'price_start' => $this->price_start,
            'spot_type' => $this->spot_type,
            'brief_desc' => $this->brief_desc,
            'customtel' => $this->customtel,
            'details' => $this->details,
            'address' => $this->address,
            'images' => $this->images,
            'opentime' => $this->opentime,
        );

        $domain = new DomainCURD();
        $id = $domain->insert($newData);
        $result = false;
        $result_content = '添加失败，请稍后重试';
        if ($id > 0) {
            $result = true;
            $result_content = '添加成功';
        }
        $rs['result'] = $result;
        $rs['msg'] = $result_content;
        $rs['id'] = $id;
        return $rs;
    }

    /**
     * 更新数据
     * @desc 根据ID更新数据库中的一条纪录数据
     * @return int code 更新的结果，1表示成功，0表示无更新，false表示失败
     */
    public function update()
    {
        $rs = array();
        $title = $this->title;
        $small_img = $this->small_img;
        $sort = $this->sort;
        $score = $this->score;
        $tag = $this->tag;
        $price_start = $this->price_start;
        $spot_type = $this->spot_type;
        $brief_desc = $this->brief_desc;
        $customtel = $this->customtel;
        $details = $this->details;

        $address = $this->address;
        $images = $this->images;
        $opentime = $this->opentime;
        if ($address) {
            $newData['address'] = $address;
        }
        if ($images) {
            $newData['images'] = $images;
        }
        if ($opentime) {
            $newData['opentime'] = $opentime;
        }
        if ($price_start) {
            $newData['price_start'] = $price_start;
        }
        if ($spot_type) {
            $newData['spot_type'] = $spot_type;
        }
        if ($brief_desc) {
            $newData['brief_desc'] = $brief_desc;
        }
        if ($customtel) {
            $newData['customtel'] = $customtel;
        }
        if ($details) {
            $newData['details'] = $details;
        }

        if ($title) {
            $newData['title'] = $title;
        }
        if ($small_img) {
            $newData['small_img'] = $small_img;
        }
        if ($sort) {
            $newData['sort'] = $sort;
        }
        if ($score) {
            $newData['score'] = $score;
        }
        if ($tag) {
            $newData['tag'] = $tag;
        }
        $domain = new DomainCURD();
        $code = $domain->update($this->id, $newData);
        $result = false;
        $result_content = '更新失败，请稍后重试';
        if ($code > 0) {
            $result = true;
            $result_content = '更新成功';
        }
        $rs['result'] = $result;
        $rs['msg'] = $result_content;
        $rs['code'] ="0";
        return $rs;
    }

    /**
     * 获取数据
     * @desc 根据ID获取数据库中的一条纪录数据
     * @return int      id          主键ID
     * @return string   title       标题
     * @return string   content     内容
     * @return int      state       状态
     * @return string   post_date   发布日期
     */
    public function get()
    {
        $domain = new DomainCURD();
        $data = $domain->get($this->id);

        return $data;
    }

    /**
     * 删除数据
     * @desc 根据ID删除数据库中的一条纪录数据
     * @return int code 删除的结果，1表示成功，0表示失败
     */
    public function delete()
    {
        $rs = array();
        $newData['status_id'] = '2';
        $domain = new DomainCURD();
        $code = $domain->update($this->id, $newData);
        $result = false;
        $result_content = '删除失败，请稍后重试';
        if ($code > 0) {
            $result = true;
            $result_content = '删除成功';
        }
        $rs['result'] = $result;
        $rs['msg'] = $result_content;
        return $rs;
    }

    /**
     * 获取分页列表数据
     * @desc 根据状态筛选列表数据，支持分页
     * @return array    items   列表数据
     * @return int      total   总数量
     * @return int      page    当前第几页
     * @return int      perpage 每页数量
     */
    public function getList()
    {
        $rs = array();

        $domain = new DomainCURD();
        $list = $domain->getList($this->page, $this->perpage, $this->cityid, $this->word);

        $rs['items'] = $list['items'];
        $rs['total'] = $list['total'];
        $rs['page'] = $this->page;
        $rs['perpage'] = $this->perpage;

        return $rs;
    }
}